package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.MaterialMapper;
import com.ruoyi.system.service.IMaterialService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 材料Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-16
 */
@Service
public class MaterialServiceImpl implements IMaterialService {
    @Resource
    private MaterialMapper materialMapper;

    /**
     * 查询材料
     *
     * @param mId 材料ID
     * @return 材料
     */
    @Override
    public Material selectMaterialById(Long mId) {
        return materialMapper.selectMaterialById(mId);
    }

    /**
     * 查询材料明细记录列表
     *
     * @param material 材料
     * @return 材料
     */
    @Override
    public List<MaterialEight> selectMaterialList(Material material, Integer pid) {
        HashMap<String, Object> map = new HashMap();
        map.put("material", material);
        map.put("pid", pid);
        List<MaterialEight> materials = materialMapper.selectMaterialList(map);
        return materials;
    }
    /** @Description: 添加材料
     * @Param: [pid, mid]
     * @returns: int
     * @Author: Administrator
     * @Date: 2020/12/22 17:13
     **/

    /**
     * @Description: 添加材料
     * @Param: [pid, mid]
     * @returns: int
     * @Author: Administrator
     * @Date: 2020/12/22 17:13
     **/

    @Override
    public int deleteMaterial(long pid, long mid) {
        HashMap<String, Object> map = new HashMap();
        Material material = new Material();
        map.put("material", material);
        map.put("pid", pid);
        map.put("mid", mid);
        List<MaterialSeven> materials = materialMapper.selectmaterialdetailsList(map);
        if (materials.size() > 0) {
            return 0;
        } else {
            return materialMapper.deleteMaterial(map);
        }
    }

    @Override
    public List<Long> selectCount(Long mid, Integer pid) {
        Double out = materialMapper.selectOutboundVolume(mid, pid);
        Double in = materialMapper.selectInboundVolume(mid, pid);
        List list = new ArrayList();
        list.add(out);
        list.add(in);
        return list;
    }

    @Override
    public int insertMaterialproject(MaterialThree materialThree) {
        if (materialMapper.selectProjectsMaterial(materialThree) > 0) {
            return 0;
        } else {
            System.out.println(materialThree);
            return materialMapper.insertMaterialproject(materialThree);
        }
    }

    @Override
    public List<HashMap> projectCost(Long pId) {
        return materialMapper.projectCost(pId);
    }

    /**
     * 查询材料列表
     *
     * @param material 材料
     * @return 材料
     */
    @Override
    public List<Material> selectMaterialListQuery(Material material, Integer pid) {
        HashMap<String, Object> map = new HashMap();
        map.put("material", material);
        map.put("pid", pid);
        List<Material> materials = materialMapper.selectMaterialListQuery(map);
        for (int i = 0; i < materials.size(); i++) {
            Long getmId = materials.get(i).getmId();
            double out = 0L;
            double in = 0L;
            if (null != materialMapper.selectOutboundVolume(getmId, pid)) {
                out = materialMapper.selectOutboundVolume(getmId, pid);
            }
            if (null != materialMapper.selectInboundVolume(getmId, pid)) {
                in = materialMapper.selectInboundVolume(getmId, pid);
            }
            double result = out - in;
            materials.get(i).setmNumber(result);
        }
        return materials;
    }

    @Override
    public List<Material> queryMaterials(Material material) {
        return materialMapper.queryMaterials(material);
    }

    //查询材料的明细
    @Override
    public List<MaterialSeven> selectmaterialdetailsList(Material material, Integer pid, Integer mid) {
        HashMap<String, Object> map = new HashMap();
        map.put("material", material);
        map.put("pid", pid);
        map.put("mid", mid);
        List<MaterialSeven> materials = materialMapper.selectmaterialdetailsList(map);
        DecimalFormat df = new DecimalFormat("######0.00");
        df.setRoundingMode(RoundingMode.HALF_UP);
        for (int i = 0; i < materials.size(); i++) {
            materials.get(i).setMoney(Double.valueOf(df.format(new BigDecimal(materials.get(i).getMoney().doubleValue()+""))));
        }
        return materials;
    }

    @Override
    public List<Material> selectmaterialRecordList(Material material, Integer pid, Integer mid) {
        HashMap<String, Object> map = new HashMap();
        map.put("material", material);
        map.put("pid", pid);
        map.put("mid", mid);
        List<Material> materials = materialMapper.selectmaterialRecordList(map);
        DecimalFormat df = new DecimalFormat("######0.00");
        df.setRoundingMode(RoundingMode.HALF_UP);
        for (int i = 0; i < materials.size(); i++) {
            materials.get(i).setMoney(Double.valueOf(df.format(new BigDecimal(materials.get(i).getMoney().doubleValue()+""))));
        }
        return materials;
    }

    //领料回显
    @Override
    public List<PickCondition> searchByMid(HashMap<String, Object> map) {

        return materialMapper.searchByMid(map);
    }

    //领料
    @Override
    public boolean pick(PickCondition pickCondition) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String now = simpleDateFormat.format(new Date());
        pickCondition.setCreateTime(now);
        //去仓库减去数量
        //获取材料总量
        double total = pickCondition.getNum();
        //获取材料领料量
        double pick = pickCondition.getPickNum();
        //需要修改的量
        double pickResultNum = total - pick;

        HashMap<String, Object> map = new HashMap();
        map.put("pickResultNum", pickResultNum);
        map.put("pickCondition", pickCondition);
        //定义变量存结果
        int b;
        if (pickResultNum == 0) {
            //根据仓库名字查仓库id，将仓库id手动封装到实体类
            String wname = pickCondition.getWname();
            Long i = materialMapper.selectWarehouseByWname(wname);
            pickCondition.setId(i);
            //领完了 去仓库修改数量
//            int delete = materialMapper.deleteByCondition(pickCondition);
            int delete = materialMapper.updateSum(map);
            b = delete;
        } else {
            //没领完 去仓库修改数量

            int update = materialMapper.updateSum(map);
            b = update;
        }
        String mName = pickCondition.getmName();
        //判断此种材料是不是电缆
        if (mName.equals("低压电缆") || mName.equals("高压电缆") || mName.equals("控制电缆")) {
            //去项目中加一条材料
            int insertProMat = materialMapper.insertMaterialByCondition(pickCondition);
        } else {
            //不是电缆的话，更新数量
            //先找有没有同种材料
            PickCondition pickCondition1 = materialMapper.selectSameMat(pickCondition);

            if (null == pickCondition1) {
                //证明项目中没有此种材料直接插入即可
                pickCondition.setNum(pickCondition.getPickNum());
                int insertProMat = materialMapper.insertMaterialByCondition(pickCondition);
            } else {
                //证明材料中已经有了这种材料，修改数量
                Double pickNum = pickCondition.getPickNum();
                Double num = pickCondition1.getNum();
                Double result = num + pickNum;
                HashMap<String, Object> map1 = new HashMap();
                map1.put("pickResultNum", result);
                map1.put("pickCondition", pickCondition1);
                materialMapper.updateMatSum(map1);
            }

        }


        //从材料操作记录中插入操作记录（领料记录）
        int insertProRec = materialMapper.insertPickRecord(pickCondition);

        //从仓库操作记录中插入数据
        // 根据id查项目名
        Long aLong = pickCondition.getpId();
        String p_name = materialMapper.selectPnameByPid(aLong);
        //将去向改为项目名
        pickCondition.setWrWhereabouts(p_name);

        //根据仓库名字查仓库id
        Long aLong1 = materialMapper.selectWarehouseByWname(pickCondition.getWname());
        pickCondition.setId(aLong1);

        int insertWarRec = materialMapper.outFromWareHouse(pickCondition);

        boolean reslut = false;
        if (b == 1 && insertProRec == 1 && insertWarRec == 1) {
            reslut = true;
        }
        return reslut;
    }

    //退料回显
    @Override
    public List<PickCondition> selectMatById(HashMap<String, Object> map) {
        List<PickCondition> pickConditions = materialMapper.selectMatById(map);
        return pickConditions;
    }

    //退料
    @Override
    public boolean returnWare(PickCondition pickCondition) {
        //从项目中减去材料 那么有两种情况
        //第一种 退完了 直接在项目材料明细中将此材料删除
        //获取材料总量
        double total = pickCondition.getNum();
        //获取材料领料量
        double pick = pickCondition.getPickNum();
        //需要修改的量
        double pickResultNum = total - pick;
        //定义变量存结果
        int b;
        if (pickResultNum == 0) {
            //退完了 去项目明细删记录
            HashMap<String, Object> map = new HashMap();
            map.put("pickResultNum", pickResultNum);
            map.put("pickCondition", pickCondition);
            int delete = materialMapper.updateMatSum(map);
            b = delete;
        } else {
            //没退完 去项目材料修改数量
            HashMap<String, Object> map = new HashMap();
            map.put("pickResultNum", pickResultNum);
            map.put("pickCondition", pickCondition);
            int update = materialMapper.updateMatSum(map);
            b = update;
        }

        //去仓库中加材料
        //根据仓库名字查仓库id
        String wname = pickCondition.getWrWhereabouts();
        Long i = materialMapper.selectWarehouseByWname(wname);
        pickCondition.setId(i);
        int add = materialMapper.addMat(pickCondition);
        //先查看这个仓库有没有这种材料
        HashMap<String, Object> map = new HashMap();
        Long aLong = pickCondition.getmId();
        map.put("mId", aLong);
        map.put("wId", i);
        if (materialMapper.selectMat(map) != 1) {
            //证明仓库原先没有这种材料
            materialMapper.insertMat(map);
        }
        //在项目操作中加一条退料记录
        //将去向改为仓库名字
        pickCondition.setWrWhereabouts(wname);
        int addProRecord = materialMapper.addProRecord(pickCondition);

        //在仓库操作记录中加一条入库记录
        int addWareRecord = materialMapper.addWareRecord(pickCondition);
        boolean reslut = false;
        if (b == 1 && add == 1 && addProRecord == 1 && addWareRecord == 1) {
            reslut = true;
        }
        return true;
    }

    //退料回显所有仓库
    @Override
    public List<String> sreturnWares() {
        return materialMapper.sreturnWares();
    }

}
